<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>数组定义</title>
    <script type="text/javascript">
      //数组定义的4种方式
      //1.直接[]赋值
      var cars = ["Audi", "BMW", "volvo", 100];
      console.log("cars=" + cars);
      console.log(cars[1]);//表示访问 cars 数组的第二个元素

      //2.先定义空数组再赋值
      var cars2 = [];
      cars2[0] = "奥迪";
      cars2[1] = "宝马";
      console.log("cars2=" + cars2);

      //3.new Array(xx,xx,...)
      var cars3 = new Array("Audi", "BMW");
      console.log("cars3=" + cars3);

      //4.new Array()再赋值
      var cars4 = new Array();
      cars4[0] = "法拉利";
      cars4[1] = "保时捷"; //相当于给数组扩容
      cars4[0] = "宾利";  //相当于替换

      cars4[8] = "红旗";  //如果跳过了下标给赋值，那么中间没有赋值的元素为undefined
      console.log("cars4[5]=" + cars4[5]);  //undefined
      console.log("cars4=" + cars4);
    </script>
  </head>

  <body>

  </body>
</html>